from elasticsearch import Elasticsearch
import json
import os

# 连接到 Elasticsearch
es = Elasticsearch("http://192.168.49.2:32678/")


def get_log(start_time, end_time,log_path):

    # print(start_time)
    log_date = start_time.split("T")[0].replace("-", ".")  # 将日期格式转换为 Elasticsearch 索引格式
    index_name = f"logstash-{log_date}"  # 构建索引名称

    print(f"开始提取日志数据，索引名称: {index_name}")



    # 定义查询条件，筛选出 namespace 为 tt 的数据
    query = {
        "query": {
            "bool": {
                "must": [
                    {"term": {"kubernetes.namespace_name": "tt"}},
                    {"term": {"kubernetes.container_name": "service"}},
                    {
                        "range": {
                            "@timestamp": {
                                # "gte": "2025-05-26T11:49:00",
                                # "lte": "2025-05-26T12:59:59"
                                "gte": start_time,
                                "lte": end_time
                            }
                        }
                    }
                ]
            }
        },
        "size": 10000
    }

    # print(f"查询条件: {json.dumps(query, indent=2)}")

    # 使用 Scroll API 来获取所有匹配的数据
    scroll_results = []
    resp = es.search(index=index_name, body=query, scroll="1m")
    # print(resp)
    scroll_id = resp['_scroll_id']

    # 处理第一批次的数据
    scroll_results.extend(resp['hits']['hits'])

    # 循环获取剩余批次的数据
    while True:
        resp = es.scroll(scroll_id=scroll_id, scroll="1m")
        if not resp['hits']['hits']:
            break
        scroll_results.extend(resp['hits']['hits'])
        scroll_id = resp['_scroll_id']

    # 释放 Scroll 上下文
    es.clear_scroll(scroll_id=scroll_id)

    # 按服务名分组处理数据，格式为 "service_name": []
    service=["ts-auth-service","ts-assurance-service","ts-basic-service","ts-config-service","ts-contacts-service","ts-food-map-service","ts-food-service","ts-inside-payment-service","ts-notification-service","ts-order-other-service","ts-order-service","ts-payment-service","ts-preserve-service","ts-price-service","ts-route-service","ts-route-plan-service","ts-seat-service","ts-security-service","ts-ticketinfo-service","ts-train-service","ts-travel-service","ts-travel-plan-service","ts-travel2-service","ts-user-service","ts-cancel-service","ts-station-service","ts-verification-code-service"]


    # processed_data = {'ts-admin-basic-info-service': [], 'ts-admin-order-service': [], 'ts-admin-route-service': [], 'ts-admin-travel-service': [], 'ts-admin-user-service': [], 'ts-assurance-service': [], 'ts-auth-service': [], 'ts-basic-service': [], 'ts-cancel-service': [], 'ts-config-service': [], 'ts-consign-price-service': [], 'ts-consign-service': [], 'ts-contacts-service': [], 'ts-execute-service': [], 'ts-food-map-service': [], 'ts-food-service': [], 'ts-inside-payment-service': [], 'ts-news-service': [], 'ts-notification-service': [], 'ts-order-other-service': [], 'ts-order-service': [], 'ts-payment-service': [], 'ts-preserve-other-service': [], 'ts-preserve-service': [], 'ts-price-service': [], 'ts-rebook-service': [], 'ts-route-plan-service': [], 'ts-route-service': [], 'ts-seat-service': [], 'ts-security-service': [], 'ts-station-service': [], 'ts-ticket-office-service': [], 'ts-ticketinfo-service': [], 'ts-train-service': [], 'ts-travel-plan-service': [], 'ts-travel-service': [], 'ts-travel2-service': [], 'ts-user-service': [], 'ts-verification-code-service': [], 'ts-voucher-service': []}

    processed_data = {service_name: [] for service_name in service}

    for hit in scroll_results:
        source = hit['_source']

        # 假设每个文档中有 service_name 字段来标识服务
        service_name = source.get("kubernetes", {}).get("container_name", "unknown")
        if service_name not in processed_data:
            processed_data[service_name] = []
        # 可以根据需要选择保留哪些字段
        processed_data[service_name].append(source["log"])


    # 将处理后的数据保存到文件

    log_path = os.path.join(log_path, 'logs.json')

    with open(log_path, 'w', encoding='utf-8') as f:
        json.dump(processed_data, f, ensure_ascii=False, indent=4)

    print(f"成功提取并处理 {len(scroll_results)} 条数据")
